/**
 * 
 */
package com.lanswon.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.lanswon.common.constants.Constants;
import com.lanswon.common.enums.MessageType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.dao.BusMapper;
import com.lanswon.entity.BusVO;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: BusService.java
 * @Description: 该类的功能描述
 *
 * @version: v1.0.0
 * @author: zm
 * @date: 2018年5月29日 下午5:07:11
 *
 */
@Service
public class BusService {
	@Autowired
	private BusMapper busMapper;

	/**
	 * @param multipartFile
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@Transactional(rollbackFor = Exception.class)
	public Map<String, Object> upLoadBus(MultipartFile multipartFile) {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		String imgName = multipartFile.getOriginalFilename();
		if(StringUtil.isEmpty(imgName)){
			resultMap.put("code", 1);
			resultMap.put("message", "请选择上传的文件");
			return resultMap;
		}
		String fileType = imgName.substring(imgName.lastIndexOf(".") + 1, imgName.length());
		if (("txt").indexOf(fileType) == -1) {
			resultMap.put("code", 1);
			resultMap.put("message", "请上传txt文件");
			return resultMap;
		}

		String[] strs = null;
		String[] detail = null;
		String num = null;
		String upDepartureStation = null;
		String upStart = null;
		String upEnd = null;
		String downDepartureStation = null;
		String downStart = null;
		String downEnd = null;
		Integer upCount = 0;
		Integer downCount = 0;
		String upStations = "";
		String downStations = "";
		try {
			// 删除
			busMapper.deleteAll();
			InputStream in = multipartFile.getInputStream();
			BufferedReader bf = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String line = bf.readLine();
			BusVO busVO = new BusVO();
			while ((line = bf.readLine()) != null) {
				strs = line.split("\\|");

				num = strs[0];// 线路
				busVO.setBusName(num);

				detail = strs[2].split(",");
				upStart = detail[1];// 上行始发
				upEnd = detail[2];// 上行末班
				upCount = Integer.valueOf(strs[4]);// 上行总数
				upStations = strs[5];// 上行站点

				detail = strs[1].split(",");
				downStart = detail[1];// 下行始发
				downEnd = detail[2];// 下行末班
				downCount = Integer.valueOf(strs[7]);// 下行总数
				downStations = strs[8];// 下行站点

				detail = strs[3].split(",");
				upDepartureStation = detail[0];// 上行首站
				downDepartureStation = detail[1];// 上行末站
				// 上行
				busVO.setBusFirstStation(upDepartureStation);
				busVO.setBusFirstTime(upStart);
				busVO.setBusDirection("1");
				busVO.setBusLastStation(downDepartureStation);
				busVO.setBusLastTime(upEnd);
				busVO.setBusStationDetail(upStations);
				busMapper.insert(busVO);

				detail = strs[6].split(",");
				upDepartureStation = detail[0];// 下行首站
				downDepartureStation = detail[1];// 下行末站
				// 下行
				busVO.setBusFirstStation(upDepartureStation);
				busVO.setBusFirstTime(downStart);
				busVO.setBusDirection("2");
				busVO.setBusLastStation(downDepartureStation);
				busVO.setBusLastTime(downEnd);
				busVO.setBusStationDetail(downStations);
				busMapper.insert(busVO);
			}
			resultMap.put("code", 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * @return
	 */
	public Map<String, Object> showBus(String busName) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		JSONArray jsonArray = new JSONArray();
		try {
			if (!StringUtil.isEmpty(busName)) {
				Matcher matcher = Constants.PATTERN.matcher(busName);
				busName = matcher.replaceAll("");
				BusVO busVO = busMapper.showByNumAndDir(busName, "1");
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("name", busVO.getBusName());
				jsonObject.put("direction", "1");
				jsonObject.put("firstStation", busVO.getBusFirstStation());
				jsonObject.put("firstTime", busVO.getBusFirstTime());
				jsonObject.put("lastStation", busVO.getBusLastStation());
				jsonObject.put("lastTime", busVO.getBusLastTime());
				jsonArray.add(jsonObject);
			} else {
				List<BusVO> busVOs = busMapper.selectBus();
				for (BusVO busVO : busVOs) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("name", busVO.getBusName());
					jsonObject.put("direction", "1");
					jsonObject.put("firstStation", busVO.getBusFirstStation());
					jsonObject.put("firstTime", busVO.getBusFirstTime());
					jsonObject.put("lastStation", busVO.getBusLastStation());
					jsonObject.put("lastTime", busVO.getBusLastTime());
					jsonArray.add(jsonObject);
				}
			}
			resultMap.put("code", 0);
			resultMap.put("message", MessageType.MT007);
			resultMap.put("data", jsonArray);
		} catch (Exception e) {
			resultMap.put("code", 1);
			resultMap.put("message", MessageType.MT008);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * @param busName
	 * @param directionz
	 * @return
	 */
	public Map<String, Object> showByNumAndDir(String busName, String direction) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			BusVO busVO = busMapper.showByNumAndDir(busName, direction);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("name", busVO.getBusName());
			jsonObject.put("direction", direction);
			jsonObject.put("firstStation", busVO.getBusFirstStation());
			jsonObject.put("firstTime", busVO.getBusFirstTime());
			jsonObject.put("lastStation", busVO.getBusLastStation());
			jsonObject.put("lastTime", busVO.getBusLastTime());
			jsonObject.put("stationDetail", busVO.getBusStationDetail());
			resultMap.put("code", 0);
			resultMap.put("message", MessageType.MT007);
			resultMap.put("data", jsonObject);
		} catch (Exception e) {
			resultMap.put("code", 1);
			resultMap.put("message", MessageType.MT008);
			e.printStackTrace();
		}
		return resultMap;
	}
}
